package tk3.project;

import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import tk3.project.uploadservice.Client;

public class PictureReceiverThread extends Thread implements LocationListener{

	private boolean gpsBased = true;

	private String stream;
	private String password;

	private double lon;
	private double lat;

	private boolean running = true;

	private Client client;

	private String ip = "";
	private String path = "";
	private int port = 0;
	private int sleeptime=20;//in sec
	
	/**
	 * 
	 * @param c - The global client object
	 * @param updateInterval - how often the pictures shall be pulled
	 */
	public PictureReceiverThread(Client c, int updateInterval){
		client=c;
		sleeptime=updateInterval;
	}

	public void run() {
		
		ip = Settings.getActiveInstance().getIP();
		port = Settings.getActiveInstance().getPort();
		path = Settings.getActiveInstance().getPath();

		while (running()) {
			try {
				sleep(sleeptime*60000);

				String stream = Settings.getActiveInstance().getStreamName();
				String password = Settings.getActiveInstance().getStreamPassword();
				int radius = Settings.getActiveInstance().getRadius();
				int duration = Settings.getActiveInstance().getDuration();

				gpsBased = Settings.getActiveInstance().isGpsBased();

				if (!gpsBased) {
					client.getPictures(stream, password);
				} else {
					client.getPictures(lat, lon, radius);
				}

			} catch (InterruptedException e) {
				e.printStackTrace();
				client.stopClient();
			}
		}
	}



	

	
	
	public synchronized void stopReceiver(){
		running=false;
	}
	
	public synchronized boolean running(){
		return running;
	}
	/**
	 * Updates the Location lon/lat
	 */
	public void onLocationChanged(Location location) {
		lat = location.getLatitude();
		lon = location.getLongitude();
		
	}

	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}

}
